package com.winchain.provider.expiration.cmd;

import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbDctQtService;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillExpResDctQtCmd implements ICommand<Bill<CbRec>> {
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private CbDctQtService cbDctQtService;

	public boolean execute(CommandContext<Bill<CbRec>> context) throws Exception {
		Bill<CbRec> bill = (Bill) context.getSource();
		CbOpt operation = (CbOpt) context.getValue("Operation");
		CbRec rec = bill.getRec();
		String curState = operation.getCurState();
		if (("S011".equals(curState)) || ("S012".equals(curState)) || ("S035".equals(curState))) {
			List<String> dctStates = new ArrayList();
			dctStates.add("1");
			dctStates.add("2");
			CbDct dct = this.cbDctService.selectDctByBillIdAndStates(rec.getBillId(), dctStates);
			if (!StringUtil.emptyAndNull(dct.getQtId())) {
				if ("S012".equals(curState)) {
					this.cbDctQtService.res(dct.getQtId(), dct.getDctAmt());
				} else {
					this.cbDctQtService.unfreeze(dct.getQtId(), dct.getDctAmt());
				}
			}
		}
		return false;
	}
}
